//
//  MyWorkController.m
//  PropertyMananger
//
//  Created by bh on 17/1/20.
//  Copyright © 2017年 BH. All rights reserved.
//

#import "MyWorkController.h"
#import "MyPublicController.h"
#import "MyEletricsController.h"
#import "SGSegmentedControl.h"
#import "RightItemView.h"
#import "MyComplainController.h"

@interface MyWorkController ()<SGSegmentedControlStaticDelegate, UIScrollViewDelegate>


@property (nonatomic,strong) UIButton * selectedTimeBtn;//时间选择
@property (nonatomic,strong) UILabel * timeLabel;//时间

@property (nonatomic,assign) BOOL isSelected;

@property (nonatomic,copy) NSString * timeStr;

@property (nonatomic,copy) NSString * selectedTimeStr;

@property (nonatomic,assign) NSInteger index;

@property (nonatomic,strong) RightItemView * rightItemView;


@property (nonatomic,strong) MyPublicController * publicVC;
@property (nonatomic,strong) MyEletricsController * hydroVC;
@property (nonatomic,strong) MyComplainController * complainVC;


@end

@implementation MyWorkController
- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBar.hidden = NO;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
   [self initTitleViewName:@"我的工单"];
    
    [self getYearAndMonthData];
    
    [self createSegmentUI];
    
    self.rightItemView = [[RightItemView alloc]initWithFrame:CGRectMake(0, 0, 100, 44)];
    
    UIBarButtonItem * rightButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.rightItemView];
   self.navigationItem.rightBarButtonItem = rightButtonItem;
    
   [self.rightItemView.selectedTimeBtn addTarget:self action:@selector(rightButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    self.rightItemView.timeLabel.text = self.selectedTimeStr;
    
    
    if (self.QueryTypeTag == 100) {
        
        self.QueryType = @"1";
    }else{
    
        self.QueryType = @"0";
    }
    
}
- (void)rightButtonClick:(UIButton *)button{

    DLog(@"点击选择时间");
    
    
        __block MyWorkController *weakSelf = self;
        dateTimePickerView = [[HcdDateTimePickerView alloc] initWithDatePickerMode:DatePickerYearMonthMode defaultDateTime:[[NSDate alloc]initWithTimeIntervalSinceNow:1000]];
    
        dateTimePickerView.topViewColor = BHGrayColor;
        dateTimePickerView.buttonTitleColor = Theme_Color;
        dateTimePickerView.clickedOkBtn = ^(NSString * datetimeStr){
            NSLog(@"%@", datetimeStr);
            
       //     weakSelf.timeStr = datetimeStr;
            
          //  weakSelf.publicVC.selectedTimeStr = datetimeStr;
            
            NSArray * timeArr = [datetimeStr componentsSeparatedByString:@"-"];
            NSString * timeStr = nil;
            if ([timeArr[1]integerValue] <= 9) {
                
            timeStr = [NSString stringWithFormat:@"%@-0%@",timeArr[0],timeArr[1]];
            }else{
            timeStr = [NSString stringWithFormat:@"%@-%@",timeArr[0],timeArr[1]];
            }
            
              weakSelf.rightItemView.timeLabel.text = timeStr;
            
            weakSelf.timeStr = timeStr;
            
            DLog(@"===%@",weakSelf.timeStr);
            
            NSDictionary * dict = @{@"key":timeStr};
            
            [[NSNotificationCenter defaultCenter]postNotificationName:timeNoticeCenter object:nil userInfo:dict];
            
            DLog(@"--------- r%ld",weakSelf.index);
            
        };
    
        if (dateTimePickerView) {
            [[UIApplication sharedApplication].keyWindow addSubview:dateTimePickerView];
            [dateTimePickerView showHcdDateTimePicker];
        }
    
}
- (void)createSegmentUI
{
    
    self.publicVC = [[MyPublicController alloc]init];
    self.publicVC.QueryType = self.QueryType;
    self.publicVC.selectedTimeStr = self.timeStr;
    [self addChildViewController:self.publicVC];
    
    self.hydroVC = [[MyEletricsController alloc]init];
    self.hydroVC.QueryType = self.QueryType;
    self.hydroVC.selectedTimeStr = self.timeStr;
    DLog(@"==%@",self.timeStr);
    [self addChildViewController:self.hydroVC];
    
     self.complainVC = [[MyComplainController alloc]init];
    self.complainVC.QueryType = self.QueryType;
    [self addChildViewController:self.complainVC];
    
    NSArray * controllers = @[self.publicVC,self.hydroVC,self.complainVC];
    
    NSArray *titleArray =@[@"公共报修",@"水电报修",@"投诉单"];
    self.bottomSView = [[SGSegmentedControlBottomView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    self.bottomSView.childViewController = controllers;
    self.bottomSView.backgroundColor = [UIColor clearColor];
    self.bottomSView.delegate = self;
    
    //_bottomView.scrollEnabled = NO;
    [self.view addSubview:self.bottomSView];
    
    self.topSView = [SGSegmentedControlStatic segmentedControlWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44) delegate:self childVcTitle:titleArray indicatorIsFull:NO];
    // 必须实现的方法
    [self.topSView SG_setUpSegmentedControlType:^(SGSegmentedControlStaticType *segmentedControlStaticType, NSArray *__autoreleasing *nomalImageArr, NSArray *__autoreleasing *selectedImageArr) {
        
    }];
    self.topSView.selectedIndex = 0;
    [self.view addSubview:self.topSView];
}

- (void)SGSegmentedControlStatic:(SGSegmentedControlStatic *)segmentedControlStatic didSelectTitleAtIndex:(NSInteger)index {
    NSLog(@"index - - %ld", (long)index);
    self.index = index;
    
    // 计算滚动的位置
    CGFloat offsetX = index * self.view.frame.size.width;
    self.bottomSView.contentOffset = CGPointMake(offsetX, 0);
    [self.bottomSView showChildVCViewWithIndex:index outsideVC:self];
}
#pragma mark - 获取系统时间
- (void)getYearAndMonthData{
    
    NSDate *date = [NSDate date];
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    NSTimeInterval time = [zone secondsFromGMTForDate:date];
    NSDate *dateNow = [date dateByAddingTimeInterval:time];
    NSString * timeDataStr = [NSString stringWithFormat:@"%@",dateNow];
    DLog(@" === %@",dateNow);
    
    NSArray * arr = [timeDataStr componentsSeparatedByString:@"+"];
    DLog(@"日期格式化 == %@",arr[0]);
    
    NSArray * timeArr = [timeDataStr componentsSeparatedByString:@"-"];
    NSString * timeStr = [NSString stringWithFormat:@"%@-%@",timeArr[0],timeArr[1]];
    DLog(@"拼接好的时间===%@",timeStr);
    self.selectedTimeStr = timeStr;
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    // 计算滚动到哪一页
    NSInteger index = scrollView.contentOffset.x / scrollView.frame.size.width;
    
    // 1.添加子控制器view
    [self.bottomSView showChildVCViewWithIndex:index outsideVC:self];
    
    // 2.把对应的标题选中
    [self.topSView changeThePositionOfTheSelectedBtnWithScrollView:scrollView];
}


@end
